package com.cn.yugan.Engine.Json;

import com.cn.yugan.Engine.Beans.ArticleListInfo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

/**
 * Created by Rover on 2016/10/11 .
 * Email 357319517@qq.com
 */
public class ArticleListJson {

    public static ArrayList<ArticleListInfo> json2list(byte[] bytes) {
        try {
            ArrayList<ArticleListInfo> list = new ArrayList<>();
            JSONObject json = new JSONObject(new String(bytes, "UTF-8"));
            boolean success = json.getBoolean("success");
            try {
                String count = json.getString("count");
            } catch (Exception e) {

            }

            JSONArray arr = json.getJSONArray("data");
            for (int j = 0; j < arr.length(); j++) {
                try {
                    JSONObject obj = arr.getJSONObject(j);
                    ArticleListInfo info = new ArticleListInfo();
                    try {
                        info.setAid(obj.getString("aid"));
                    } catch (JSONException e) {
                    }
                    try {
                        info.setTitle(obj.getString("title"));
                    } catch (JSONException e) {
                    }
                    try {
                        info.setTime(obj.getString("time"));
                    } catch (JSONException e) {
                    }
                    try {
                        info.setComment(obj.getString("comment"));
                    } catch (JSONException e) {
                    }
                    try {
                        info.setClick(obj.getString("click"));
                    } catch (JSONException e) {
                    }
                    try {
                        info.setAuthor(obj.getString("author"));
                    } catch (JSONException e) {
                    }
                    try {
                        info.setIntro(obj.getString("intro"));
                    } catch (JSONException e) {
                    }
                    try {
                        info.setImg(obj.getString("img"));
                    } catch (JSONException e) {
                    }
                    try {
                        info.setStatus(obj.getInt("status"));
                    } catch (JSONException e) {
                    }
                    list.add(info);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            if (list.isEmpty()) {
                //空逻辑
                return null;
            } else {
                return list;
            }
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }


}
